#include "comm/my_hrtime.h"
#include "comm/my_log.h"

void test_hrtime()
{
    int64_t cache_time = get_cache_time();
    if (cache_time == -1) {
        MY_LOG_FATAL("except > 0 but got %d", cache_time);
    }

    MY_LOG_DEBUG("cache_time: %d", cache_time);

    int64_t cur_time = get_latest_time();
    cache_time = get_cache_time();

    if (cur_time == 0 || cur_time != cache_time) {
        MY_LOG_FATAL("except cur_time = cache_time, but cur_time = %d, cache_time = %d",
            cur_time, cache_time);
    }

    int64_t msec = my_usec_2_msec(cache_time);
    if (msec != cur_time / 1000) {
        MY_LOG_FATAL("except msec = %d, but got %d", cache_time / 1000, msec);
    }

    int64_t sec = my_usec_2_sec(cache_time);
    if (sec != cur_time / 1000 / 1000) {
        MY_LOG_FATAL("except sec = %d, but got %d", cache_time / 1000 / 1000, sec);
    }

    my_timespec_t ts;
    my_usec_2_ts(cache_time, &ts);
    int64_t us = my_ts_2_usec(&ts);
    if (us != cache_time) {
        MY_LOG_FATAL("except us = %d, but got %d", cache_time, us);
    }
}

int main()
{
    test_hrtime();

    MY_LOG_DEBUG("test pass");
    return 0;
}